<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="24" :xs="24">
        <!-- 搜索框 -->
        <el-form ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="横向课题等级" prop="leader" label-width="115px">
            <el-select
              placeholder="请选择课题等级"
              clearable
              style="width: 240px"
              v-model="queryParams.subjectLevel"
              size="small"
            >
              <el-option
                v-for="item in subjectLevel"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            label="横向课题类型"
            prop="category"
            label-width="115px"
          >
            <el-input
              v-model="queryParams.subjectType"
              placeholder="请输入横向课题类型"
              clearable
              size="small"
              style="width: 240px"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="Search"
              size="mini"
              @click="handleQuery"
              >搜索</el-button
            >
            <el-button icon="Refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
            <el-button icon="Tickets" size="mini" @click="handleRef"
              >参考</el-button
            >
          </el-form-item>
        </el-form>
        <!-- 操作按钮 -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="Plus"
              size="mini"
              @click="handleAdd"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="Edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Check"
              size="mini"
              :disabled="single"
              @click="handleDelete"
              >删除</el-button
            >
          </el-col>
          <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
        <!-- 展示列表 -->
        <el-table
          :data="topicList"
          v-loading="loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="代码" width="80" align="center" prop="code" />
          <el-table-column
            label="课题类型"
            align="center"
            prop="subjectType"
            overflow:hidden
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="课题等级"
            align="center"
            prop="subjectLevel"
            width="90"
          />
          <el-table-column
            label="经费上限"
            align="center"
            prop="fundsCeiling"
            width="90"
            overflow:hidden
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="经费下限"
            align="center"
            prop="fundsCeiling"
            width="90"
            overflow:hidden
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="工作量MAX"
            align="center"
            prop="workloadMax"
            width="90"
          />
          <el-table-column
            label="工作量MIN"
            align="center"
            prop="workloadMin"
            width="90"
          />
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            width="100"
            overflow:hidden
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="更新时间"
            align="center"
            prop="updatedTime"
            width="160"
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createdTime"
            width="160"
          />
          <el-table-column
            label="操作"
            align="center"
            width="150"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                icon="Edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="Delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 新增或修改弹窗 -->
    <el-dialog :title="title" v-model="open" width="400px" append-to-body>
      <el-form ref="issuesDictRef" :model="form" :rules="rules" label-width="100px">
             <el-form-item label="分类代码" prop="code">
               <el-input  placeholder="请输入分类代码" v-model="form.code"/>
          </el-form-item>
        <el-form-item label="课题等级" prop="subjectLevel">
          <el-select
            placeholder="请选择课题等级"
            clearable
            style="width: 260px"
            v-model="form.subjectLevel"
          >
            <el-option
              v-for="item in subjectLevel"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="课题类型" prop="subjectType">
          <el-select
            placeholder="请选择课题类型"
            clearable
            style="width: 260px"
            v-model="form.subjectType"
          >
            <el-option
              v-for="item in subjectType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作量Max" prop="workloadMax">
          <el-input-number
            :min="0"
            placeholder="请输入工作量最大值"
            controls-position="right"
            v-model="form.workloadMax"
            style="width: 260px"
          />
        </el-form-item>
        <el-form-item label="工作量Min" prop="workloadMin">
          <el-input-number
            :min="0"
            placeholder="请输入工作量最小值"
            controls-position="right"
            v-model="form.workloadMin"
            style="width: 260px"
          />
        </el-form-item>
        <el-form-item label="经费上限" prop="fundsCeiling">
          <el-input-number
            :min="0"
            placeholder="请输入经费上限"
            controls-position="right"
            v-model="form.fundsCeiling"
            style="width: 260px"
          />
        </el-form-item>
        <el-form-item label="经费下线" prop="fundsFloor">
          <el-input-number
            :min="0"
            placeholder="请输入经费下线"
            controls-position="right"
            v-model="form.fundsFloor"
            style="width: 260px"
          />
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            autosize
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 参考表单 -->
    <el-dialog :title="title" v-model="refOpen" width="650px" append-to-body>
      <el-table :data="refData" border style="width: 100%">
        <el-table-column
          prop="number"
          label="序 号"
          width="80"
          align="center"
        />
        <el-table-column prop="paper" label="论文、成果转化" align="center" />
        <el-table-column
          prop="category"
          label="类别"
          width="100"
          align="center"
        />
        <el-table-column
          prop="workload"
          label="工作量"
          width="100"
          align="center"
        />
      </el-table>
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listAll,
  addDict,
  delDict,
  getDict,
  updateDict,
} from "@/api/nepu_dict/subject";
import { reactive } from "vue"

const refData = [
  {
    number: "1",
    paper: "横向课题到账经费500万元以上",
    category: "A",
    workload: "500",
  },
  {
    number: "2",
    paper: "横向课题到账经费200-500万元",
    category: "A",
    workload: "200-500",
  },
  {
    number: "3",
    paper: "横向课题到账经费100-200万（人文社会科学30万）元以上（含）",
    category: "A",
    workload: "100-200",
  },
  {
    number: "4",
    paper: "横向课题到账经费50-100万元（人文社会科学10-30万）",
    category: "B",
    workload: "50-100",
  },
  {
    number: "5",
    paper: "横向课题（人文社会科学）到账经费2-10万元",
    category: "B",
    workload: "10-50",
  },
];
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const title = ref("");
const refOpen = ref(false);
const open = ref(false);
const total = ref(0);
const topicList = ref([]);
const loading = ref(false);
const subjectLevel = ref([
  { value: "A1", label: "A1" },
  { value: "A2", label: "A2" },
  { value: "A3", label: "A3" },
  { value: "A4", label: "A4" },
  { value: "A5", label: "A5" },
  { value: "B1", label: "B1" },
  { value: "B2", label: "B2" },
  { value: "B3", label: "B3" },
  { value: "B4", label: "B4" },
  { value: "C1", label: "C1" },
  { value: "C2", label: "C2" },
]);
const subjectType = ref([
  { value: "自然科学", label: "自然科学" },
  { value: "社会科学", label: "社会科学" },
]);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    order: "DESC",
    sortBy: "id",
    subjectType: undefined,
    subjectLevel: undefined,
  },
   rules: {
    workloadMax: [
      { required: true, message: "不能为空", trigger: "blur" },
      { type: "number", min: 0,  message: "工作量不得小于0" },
    ],
    workloadMin: [
      { required: true, message: "不能为空", trigger: "blur" },
      { type: "number", min: 0,  message: "工作量不得小于0" },
    ],
    subjectType: [
      { required: true, message: "不能为空", trigger: "blur" },
      {
        min: 2,
        max: 50,
        message: "类型名称长度必须介于 2 和 50 之间",
        trigger: "change",
      },
    ],
    subjectLevel:[
       {required: true, message: "不能为空", trigger: "blur"}
     ],
    code:[
      {required: true, message: "不能为空", trigger: "blur" }
    ],

  },
});
const { queryParams, form , rules} = toRefs(data);
const { proxy } = getCurrentInstance();

function getList() {
  loading.value = true;
  listAll(proxy.addDateRange(queryParams.value)).then((response) => {
    topicList.value = response.content;
    total.value = response.totalElements;
    loading.value = false;
  });
}

onBeforeUpdate(() => {
  console.log(topicList.value);
});

function resetQueryParams() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    order: "DESC",
    sortBy: "id",
    subjectType: undefined,
    subjectLevel: undefined,
  };
  
}

function submitForm() {
  proxy.$refs["issuesDictRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != undefined) {
        updateDict(form.value).then((response) => {
           proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
        });
      } else {
        addDict(form.value).then((response) => {
          console.log(response);
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleQuery() {
  getList();
}

function handleUpdate(row) {
  reset();
  const issuesIds = row.id || ids.value;
  form.value.id = issuesIds;
  getDict(issuesIds).then((response) => {
    form.value = response;
    open.value = true;
    title.value = "修改";
  });
}
function handleDelete(row) {
  const issuesIds = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除课题ID为"' + issuesIds + '"的数据项?')
    .then(function () {
      return delDict(issuesIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "新增课题成果";
}
function cancel() {
  open.value = false;
}

function resetQuery() {
  resetQueryParams();
  getList();
}
function reset() {
  form.value = {
    subjectLevel: undefined,
    subjectType: undefined,
    workloadMax: 0,
    workloadMin: 0,
    remark: undefined,
    code:undefined,
    fundsCeiling: undefined,
    fundsFloor: undefined,
  };
}
/** 参考表单 */
function handleRef() {
  title.value = "参考";
  refOpen.value = true;
}
getList();
</script>

<style>
</style>